from selenium.webdriver.common.by import By

from common.utils import forum_driver


class SignUpTest:
    driver = ""
    url = ""

    def __init__(self):
        self.driver = forum_driver.driver
        self.url = "http://127.0.0.1:58080/sign-up.html"
        self.driver.implicitly_wait(5)  # 隐式等待5s
        print(f"访问网址: {self.url}")
        self.driver.get(self.url)

    # 清空注册输入框
    def input_clear(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        checkbox = self.driver.find_element(By.CSS_SELECTOR, "#policy")
        if checkbox.is_selected():
            checkbox.click()

    # 成功注册
    def signup_suc_test(self):
        self.input_clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#password_a > svg").click()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        forum_driver.get_screenshot()

    # 注册跳转登录再返回
    def signup_to_signin(self):
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.page.page-center > div > div.text-center.text-muted.mt-3 > a").click()
        forum_driver.get_screenshot()
        self.driver.back()
        self.driver.refresh()

    # 失败注册
    def signup_fail_test(self):
        self.input_clear()
        # 全部不写点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        text = self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(2) > div").text
        assert text == "用户名不能为空"
        forum_driver.get_screenshot()
        # 不勾选条款注册
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        checkbox = self.driver.find_element(By.CSS_SELECTOR, "#policy")
        class_attr = checkbox.get_attribute("class")
        assert "is-invalid" in class_attr, "勾选框校验未触发"
        forum_driver.get_screenshot()





# signup = SignUpTest()
# signup.signup_suc_test()
# signup.signup_to_signin()
# signup.signup_fail_test()
